﻿package  {
	
	import flash.events.Event;
	public class Enemy extends MobObj{

		var _player:Player;
		var speed;
		var angle:Number;
		var freezeTime:Number;
		private var defaultX:Number;
		private var defaultY:Number;
		
		public function Enemy(aX:Number,aY:Number,aWidth:Number,aHeight:Number,t:Vector.<Tile>) {
			// constructor code
			super(aX,aY,aWidth,aHeight);
			defaultX = aX;
			defaultY = aY;
			_tiles = t;
			
			speed = 1;
		}
		
		public function set player(p:Player):void{_player = p;}
		
		
		public override function Init(){
			x = defaultX;
			y = defaultY;
		}
		
		public override function Update()
		{
			super.Update();
			follow();
			
			if(this.hitTestObject(_player.whip))
			{
				Die();
				
			}
			else if(this.hitTestObject(_player.ice))
			{
				Freeze();
				_player.ice.Die();
			}
			
		}
		
		private function follow()
		{
			
				angle = Math.atan2(_player.y - y, (_player.x - x));
				VelX = speed*Math.cos(angle);
				VelY = speed*Math.sin(angle);
				//trace(angle);
		}
		
		public function Freeze()
		{
			Frozen = true;
			
			speed = 0;
			freezeTime = 100;
			//this.transform.colorTransform.color = 0x000088;
			addEventListener(Event.ENTER_FRAME, Thaw);
			
			
		}
		
		public function Thaw(e:Event)
		{
			freezeTime --;
			//trace(freezeTime);
			if(freezeTime <= 0)
			{
				speed = 1;
				Frozen = false;				
				removeEventListener(Event.ENTER_FRAME, Thaw)
			}
		}
		

	}
	
}
